<?php

	class Router 
	{	
		public $folder;
		public $controller;
		public $action;
		public $name = '';
		
		private $params = array();
	
		public function __construct()
		{
			$this->folder = DEFAULT_APPLICATION;
			$this->controller = DEFAULT_CONTROLLER;
			$this->action = DEFAULT_ACTION;

			$request_split = preg_split("/\?/", $_SERVER['REQUEST_URI']);

			// store the value for route comparison later
			$uri = $request_split[0];
			
			// get controller and action
			$route_array = preg_split("/\//", $uri, 0, PREG_SPLIT_NO_EMPTY);

			// determine if the last param is the name, and if so, grab it, and unset it so it doesn't end up as a param
			$is_name = strpos(end($route_array), '.');
			if( is_name === false )
			{
				$this->name = '';
			}
			else
			{
				$this->name = reset(explode('.', end($route_array)));	
			}
			
			// process this route and populate $this
			foreach( $route_array as $index => $param )
			{
				switch( $index )
				{
					case 0:
						$result = strtolower($param);
						$breakpos = strpos($result, '-');
						if( $breakpos !== false )
						{
							$tmp = explode('-', $result, 2);
							$this->folder = $tmp[0];
							$this->controller = str_replace('-','_',$tmp[1]);
							print_r($result);
							print_r($tmp);
							print_r($this->folder);
							print_r($this->controller);
						}
						else
						{
							$this->folder = $result;
							$this->controller = DEFAULT_CONTROLLER;
						}
						break;
					case 1:
						$this->action = $param;
						break;
					default:
						$this->params []= $param;
						break;
				}
			}
		
			// convert private $params into overloaded properties
			foreach ( $this->params as $key => $value )
			{
				$param_number = $key + 1;
				$temp = 'param' . $param_number;
				$this->$temp = $value;
			}
			return;
		}
	
		########################################################################################
		#
		# 	Old way of determining the route. The new way is better
		#	because it adds an infinite number of objects as 'params'
		#	and it doesn't use regex for the routing, which was hard
		#	to change and update in any case. happy coding! ~Josh
		#
		########################################################################################
		
		public function __construct_old()
		{
			$uri_request = $_SERVER['REQUEST_URI'];

			// split controller/action from parameters
			$request_split = preg_split("/\?/", $uri_request);

			// store the value for route comparison later
			$uri = $request_split[0];
			
			// get controller and action
			$route_array = preg_split("/\//", $uri, 0, PREG_SPLIT_NO_EMPTY);
					
			$this->name = reset(explode('.', end($route_array)));
			
			include CONFIG_PATH . 'Routes.config.php';
						
			# Match URI with Routing Rule
			foreach($routing_rules as $regex => $data_array) 
			{
				#do they match?
				if(preg_match($regex, $uri)) 
				{
					#set values for this Route
					foreach($data_array as $key => $value) 
					{
						switch($key) 
						{
							case 'controller':
								if($value == 'DEFAULT') 
								{
									$routing_rules[$regex][$key] = DEFAULT_CONTROLLER;
								} 
								else if($value == 'THIS') 
								{
									$routing_rules[$regex][$key] = $route_array[0];
								}
								
								$result = strtolower($routing_rules[$regex][$key]);
								
								$breakpos = strpos($result, '-');
								if( $breakpos !== false )
								{
									$tmp = explode('-', $result, 2);
									$this->folder = $tmp[0];
									$this->controller = str_replace('-','_',$tmp[1]);
								}
								else
								{
									$this->folder = $result;
									$this->controller = 'default';
								}
								break;

							case 'action':
								if($value == 'DEFAULT') 
								{
									$routing_rules[$regex][$key] = DEFAULT_ACTION;
								} 
								else if($value == 'THIS') 
								{
									$routing_rules[$regex][$key] = $route_array[1];
								}
								$this->action = str_replace('-','_',strtolower($routing_rules[$regex][$key]));
								break;
							
							case 'id':
								$this->id = $route_array[$routing_rules[$regex][$key]];
								break;
								
							default:
								break;
						}
					}
					return;	
				}
			}
			return false;			
		}
	}
?>